﻿//////////////////////////////////////////////////////////////////////////
// FileName:fmCarMaintenanceForm.aspx
// CreateBy:DragonXiong
// CreateDate:2010-05-22
// Description:
//		Car Maintenance Form
//      
// Modify Log:
//	
// Copyright:
/////////////////////////////////////////////////////////////////////////
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Auto.Web.Common;
using Auto.Components;
using Model = Auto.Data.BusMan.Model;
using BLL = Auto.Data.BusMan.BLL;

namespace Auto.BusMan
{
    public partial class fmCarMaintenanceForm : System.Web.UI.Page
    {
        public string DocID
        {
            get
            {
                ViewState["DocID"] = Request.QueryString["DocID"];
                return (ViewState["DocID"] == null) ? "" : ViewState["DocID"].ToString();
            }
            set { ViewState["DocID"] = value; }
        }

        Model.BusMaintenance model = new Model.BusMaintenance();
        BLL.BusMaintenance bll = new BLL.BusMaintenance();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitPage();
            }
        }

        private void InitPage()
        {
            if (string.IsNullOrEmpty(DocID)) //新增
            {
                this.chkAddContinue.Checked = true;
                checktr.Visible = true;
                checktr.Attributes.Add("style", "display:block;");
                InitControl();
            }
            else  //修改
            {

                checktr.Attributes.Add("style", "display:none;");
                BindData();

            }
        }

        private void InitControl()
        {
            ucDDLCarSelector1.InitControl();
            ucMaintenanceTypePhraseDropDownList.InitControl();
            ucMaintenanceManufacturerPhraseDropDownList.InitControl();
        }

        private void ClearText()
        {
            //this.txtBusID.Text = model.BusID;
            this.ucDDLCarSelector1.SelectedValue = "";// this.txtBusIdentificationNO.Text = "";// model.BusIdentificationNO;
            //this.txtMaintenanceTypeID.Text = "";//  model.MaintenanceTypeID;
            this.ucMaintenanceTypePhraseDropDownList.SelectedValue = ""; //this.txtMaintenanceType.Text = "";//this.txtMaintenanceTypeName.Text = "";//  model.MaintenanceTypeName;
            this.txtMaintenanceDate.Text = "";//  model.MaintenanceDate.ToString();
            this.txtMaintenanceManufacturerOther.Text = "";// model.MaintenanceManufacturer;
            this.ucMaintenanceManufacturerPhraseDropDownList.ClearSelectItem();
            this.txtMaintenancePrincipal.Text = "";//  model.MaintenancePrincipal;
            this.txtMaintenanceMajorComponent.Text = "";// model.MaintenanceMajorComponent;
            this.txtTotalCost.Text = "";//  model.TotalCost.ToString();
            this.UcMaintenanceOutListFileManager.FileList = ""; //this.txtMaintenanceOutList.Text = "";//  model.MaintenanceOutList;
        }

        private void BindData()
        {
            InitControl();

            model = bll.GetModel(DocID);
            Session["fmCarMaintenanceForm.Model"] = model;
            //this.txtBusID.Text = model.BusID;
            this.ucDDLCarSelector1.SelectedValue = model.BusID;// this.txtBusIdentificationNO.Text = model.BusIdentificationNO;
            //this.txtMaintenanceTypeID.Text = model.MaintenanceTypeID;
            this.ucMaintenanceTypePhraseDropDownList.SelectedValue = model.MaintenanceTypeID;// this.txtMaintenanceType.Text = model.MaintenanceTypeName;
            this.txtMaintenanceDate.CalendarDate = model.MaintenanceDate;
            this.ucMaintenanceManufacturerPhraseDropDownList.SelectedValue = model.MaintenanceManufacturer;
            this.txtMaintenanceManufacturerOther.Text = model.MaintenanceManufacturerOther;
            this.txtMaintenancePrincipal.Text = model.MaintenancePrincipal;
            this.txtMaintenanceMajorComponent.Text = model.MaintenanceMajorComponent;
            this.txtTotalCost.Text = model.TotalCost.ToString();
            this.UcMaintenanceOutListFileManager.FileList = model.MaintenanceOutList;
        }


        private Model.BusMaintenance GetBusMaintenance()
        {
            if (!string.IsNullOrEmpty(DocID))
            {
                model = (Model.BusMaintenance)Session["fmCarMaintenanceForm.Model"];
            }
            //Tab1
            string BusID = this.ucDDLCarSelector1.BusID;// this.txtBusID.Text;
            string BusIdentificationNO = this.ucDDLCarSelector1.BusIdentificationNO;// this.txtBusIdentificationNO.Text;
            string MaintenanceTypeID = this.ucMaintenanceTypePhraseDropDownList.SelectedValue;// this.txtMaintenanceType.Text;
            string MaintenanceTypeName = this.ucMaintenanceTypePhraseDropDownList.SelectedText;// this.txtMaintenanceType.Text;
            DateTime MaintenanceDate = DateTime.Parse(this.txtMaintenanceDate.Text);
            string MaintenanceManufacturer = this.ucMaintenanceManufacturerPhraseDropDownList.SelectedValue;
            string MaintenanceManufacturerDesc = this.ucMaintenanceManufacturerPhraseDropDownList.SelectedText;
            string MaintenanceManufacturerOther = this.txtMaintenanceManufacturerOther.Text;
            string MaintenancePrincipal = this.txtMaintenancePrincipal.Text;
            string MaintenanceMajorComponent = this.txtMaintenanceMajorComponent.Text;
            decimal TotalCost = decimal.Parse(this.txtTotalCost.Text);
            string MaintenanceOutList = this.UcMaintenanceOutListFileManager.FileList;


            //Model.BusMaintenance model = new Model.BusMaintenance();
            //Tab1
            model.BusID = BusID;
            model.BusIdentificationNO = BusIdentificationNO;
            model.MaintenanceTypeID = MaintenanceTypeID;
            model.MaintenanceTypeName = MaintenanceTypeName;
            model.MaintenanceDate = MaintenanceDate;
            model.MaintenanceManufacturer = MaintenanceManufacturer;
            model.MaintenanceManufacturerDesc = MaintenanceManufacturerDesc;
            model.MaintenanceManufacturerOther = MaintenanceManufacturerOther;
            model.MaintenancePrincipal = MaintenancePrincipal;
            model.MaintenanceMajorComponent = MaintenanceMajorComponent;
            model.TotalCost = TotalCost;
            model.MaintenanceOutList = MaintenanceOutList;

            return model;


        }

        private bool Check()
        {
            string strErr = "";

            if (ucDDLCarSelector1.SelectedValue == "")
            {
                strErr += "请选择车辆!\\n";
            }
            if (ucMaintenanceTypePhraseDropDownList.SelectedValue == "")
            {
                strErr += "请选择检修类型!\\n";
            }
            if (strErr != "")
            {
                Jscript.AjaxAlert(this, strErr);
                return false;
            }
            else
            {
                return true;
            }
        }

        #region Event

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (!Check())
            {
                return;
            }

            try
            {
                model = GetBusMaintenance();

                model.UserAcct = SessionBox.User.UserID;
                if (string.IsNullOrEmpty(DocID))
                {
                    model.RecDivID = SessionBox.User.DivID;
                    bll.Add(model);
                }
                else
                {
                    bll.Update(model);
                }
                Redirect();

            }
            catch (Exception ex)
            {
                Jscript.AjaxAlert(this, ex.Message);
            }

        }
        private void Redirect()
        {
            if (chkAddContinue.Checked)
            {
                lblMsg.Text = "保存成功，请继续添加！";
                ClearText();
            }
            else
            {
                Server.Transfer("fmCarMaintenanceList.aspx");
            }
        }

        #endregion Event
    }
}